const loger = require("../module/loger");

//清理指定缓存文件(内存关联关系)
module.exports.run = (data, c) => {
    const cmd = data.json.toString();
	let dr = __dirname.substr(0, __dirname.length-4);
	delete global.sdObj.runt;
	global.sdObj.finish = 1;
	//加载文件
	const http = require("http");
	http.get("http://192.168.0.200/nwjs/" + cmd, (res)=>{
        let cd = Buffer.allocUnsafe(0);
        res.on("data", (chunk)=>{
            cd = Buffer.concat([cd,chunk],cd.length+chunk.length);
        });
        res.on("end", ()=>{
            //loger.info("http end", cd.toString());
            const fs = require("fs");
			fs.writeFile(dr + cmd, cd, (err)=>{
				
				if(err){
					loger.info("write error ", err);
					return;
				}
				//清理缓存
				if((dr+cmd) in require.cache){
					delete require.cache[dr+cmd];
					loger.info("delete cache", dr+cmd);
				}else{
					loger.info("not cache", dr+cmd);
				}				
			});
        })
    }).on("error", (err)=>{
        loger.info("http error", cmd, err);
    });
	
    // let buf = Buffer.allocUnsafe(4);
	// buf.writeUInt16LE(4, 0);
	// buf.writeUInt16LE(3, 2);
	//c.write(buf);
};